﻿using HARTCalibrationTool.SerialPortManager;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HARTCalibrationTool.Commands.HART.CustomCommands
{
    public class HART135CustomCommandFitGetInfo : HART135CustomCommand
    {
        private byte _currentNum = 0;
        private string _XCMode = null;
        private ListView _listView;
        static private ListViewGroup _sCurrentGroupID; 

        public HART135CustomCommandFitGetInfo(SerialPortsBase serialPortManager, string commandName, byte cmdCode, byte currentNum, string XCMode, ListView listView) : base(serialPortManager, commandName, cmdCode)
        {
            _currentNum = currentNum;
            _XCMode = XCMode;
            _listView = listView;
        }

        private void SccollGroup()
        {
            if (this._listView.InvokeRequired)
            {
                this._listView.Invoke(new Action(SccollGroup));
            }
            else
            {
                if(_sCurrentGroupID != _listView.Groups[(byte)(CurrentGroupChannel - 1)])
                {
                    _sCurrentGroupID = _listView.Groups[(byte)(CurrentGroupChannel - 1)];

                    ListViewItem item = _sCurrentGroupID.Items[0];

                    Rectangle itemBounds = item.Bounds;

                    // 获取 ListView 可见区域的高度
                    int visibleHeight = _listView.ClientSize.Height;

                    // 滚动到目标位置
                    _listView.TopItem = _listView.Items[item.Index];

                    // 调整滚动条位置以确保项目出现在偏移位置
                    int itemHeight = itemBounds.Height;
                    int offsetInItems = 0 / itemHeight;

                    // 计算要滚动到的位置，确保项目显示在偏移位置
                    int targetTopIndex = Math.Max(0, item.Index - offsetInItems);
                    _listView.TopItem = _listView.Items[targetTopIndex];

                    // 确保项目可见
                    _listView.EnsureVisible(item.Index);
                }
            }
        }

        public override async Task ParseResponse(byte[] data)
        {
            List<string> parts = new List<string>();
            byte[] bytes = Utils.GetHARTDataFrame(data);
            string channel = ((byte)((byte)(CurrentGroupChannel - 1) * 10 + (_currentNum - 1))).ToString();

            byte[] pressure = new byte[8];
            byte[] temperature = new byte[8];

            double dPressure = 0.0;
            double dTemperature = 0.0;

            Array.Copy(bytes, 22, pressure, 0, 8);
            Array.Copy(bytes, 34, temperature, 0, 8);

            if (BitConverter.IsLittleEndian)
            { 
                Array.Reverse(pressure);
                Array.Reverse(temperature);
            }

            dPressure = BitConverter.ToDouble(pressure, 0);
            dTemperature = BitConverter.ToDouble(temperature, 0);


            MainUI.loggerDebugMsg("Rx: " + $"{Utils.HexToString(data)}");
            MainUI.loggerDebugMsg("Pressure: " + dPressure.ToString());
            MainUI.loggerDebugMsg("Temperature: " + dTemperature.ToString());

            SccollGroup();

            await Task.Run(() =>
            {
                SerialPortsBase.PrintMessage("<-- " + $"{Utils.HexToString(data)}");

                if (_XCMode == "上行程")
                {
                    if (_currentNum == 1)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 2)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 3)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 4)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 5)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 6)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 7)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 8)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 9)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                    else if (_currentNum == 10)
                    {
                        parts.Clear();
                        parts.Add(channel + "#1#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM1", parts);

                        parts.Clear();
                        parts.Add(channel + "#2#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM2", parts);
                    }
                }
                else if (_XCMode == "下行程")
                {
                    if (_currentNum == 1)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 2)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 3)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 4)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 5)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 6)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 7)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 8)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 9)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                    else if (_currentNum == 10)
                    {
                        parts.Clear();
                        parts.Add(channel + "#3#" + dPressure.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM3", parts);

                        parts.Clear();
                        parts.Add(channel + "#4#" + dTemperature.ToString());
                        SerialPortMager.NotifyObserver("HART#135#08--NUM4", parts);
                    }
                }
            });
        }
    }

    public class AutoCaliHART135CustomCommandFitGetInfo : HART135CustomCommandFitGetInfo
    {
        public AutoCaliHART135CustomCommandFitGetInfo(SerialPortsBase serialPortManager, string commandName, byte cmdCode, byte currentNum, string XCMode, ListView listView) : base(serialPortManager, commandName, cmdCode, currentNum, XCMode, listView)
        { 
        
        }

        public override async Task ParseResponse(byte[] data)
        {
            await base.ParseResponse(data);

            await Task.Run(() =>
            {
                CommandExeSuccess();
                SetCurrentCMDResponseFinishedFlg();
                //SerialPortsBase.PrintMessage("<-- " + $"{Utils.HexToString(data)}");
            });
        }
    }
}

